package edu.hawaii.myisern.action;

import java.util.ArrayList;
import java.util.List;
import edu.hawaii.myisern.model.Log;
import edu.hawaii.myisern.model.Model;
import edu.hawaii.myisern.model.MyCollaboration;
import edu.hawaii.myisern.model.MyOrganization;
import net.sourceforge.stripes.action.DontValidate;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.validation.Validate;

/**
 * New Collaboration action bean.
 * 
 * @author rcox
 * 
 */
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public class EditCollaborationActionBean extends MyIsernActionBean {

  @Validate(required = true)
  private String name;
  private String description;
  private List<String> collaboratingOrganizations = new ArrayList<String>();
  private List<String> collaborationTypes = new ArrayList<String>();
  private List<String> outcomeTypes = new ArrayList<String>();
  private List<String> years = new ArrayList<String>();

  private String newCollaboratingOrganization;
  private String newCollaborationType;
  private String newOutcomeType;
  private List<String> allYears = new ArrayList<String>();
  private String importFile;
  private String exportFile;

  /**
   * Populate all years.
   */
  public EditCollaborationActionBean() {
    allYears = new ArrayList<String>();
    for (Integer year = 1990; year <= 2010; year++) {
      allYears.add(year.toString());
    }
  }

  /**
   * @return the importFile
   */
  public String getImportFile() {
    return importFile;
  }

  /**
   * @param importFile the importFile to set
   */
  public void setImportFile(String importFile) {
    this.importFile = importFile;
  }

  /**
   * @return the exportFile
   */
  public String getExportFile() {
    return exportFile;
  }

  /**
   * @param exportFile the exportFile to set
   */
  public void setExportFile(String exportFile) {
    this.exportFile = exportFile;
  }

  /**
   * @return the allYears
   */
  public List<String> getAllYears() {
    return allYears;
  }

  /**
   * @param allYears the allYears to set
   */
  public void setAllYears(List<String> allYears) {
    this.allYears = allYears;
  }

  /**
   * Get collaboration types.
   * 
   * @return the collaborationTypes
   */
  public List<String> getCollaborationTypes() {
    return collaborationTypes;
  }

  /**
   * Set collaboration types.
   * 
   * @param collaborationTypes the collaborationTypes to set
   */
  public void setCollaborationTypes(List<String> collaborationTypes) {
    this.collaborationTypes = collaborationTypes;
  }

  /**
   * @return the collaborationTypesStr
   */
  public String getNewCollaborationType() {
    return newCollaborationType;
  }

  /**
   * @param newCollaborationType the collaborationTypesStr to set
   */
  public void setNewCollaborationType(String newCollaborationType) {
    this.newCollaborationType = newCollaborationType;
  }

  /**
   * Get Collaborating Org string.
   * 
   * @return the collaboratingOrganizationsStr
   */
  public String getNewCollaboratingOrganization() {
    return newCollaboratingOrganization;
  }

  /**
   * Set collaborating org string.
   * 
   * @param newCollaboratingOrganization the collaboratingOrganizationsStr to set
   */
  public void setNewCollaboratingOrganization(String newCollaboratingOrganization) {
    this.newCollaboratingOrganization = newCollaboratingOrganization;
  }

  /**
   * Get outcome types.
   * 
   * @return the outcomeTypesStr
   */
  public String getNewOutcomeType() {
    return newOutcomeType;
  }

  /**
   * Set outcome types.
   * 
   * @param newOutcomeType the outcomeTypesStr to set
   */
  public void setNewOutcomeType(String newOutcomeType) {
    this.newOutcomeType = newOutcomeType;
  }

  /**
   * Returns the name.
   * 
   * @return the name
   */
  public String getName() {
    return name;
  }

  /**
   * Sets the name.
   * 
   * @param name the name to set
   */
  public void setName(String name) {
    this.name = name;
  }

  /**
   * Gets the description.
   * 
   * @return the description
   */
  public String getDescription() {
    return description;
  }

  /**
   * Sets the description.
   * 
   * @param description the description to set
   */
  public void setDescription(String description) {
    this.description = description;
  }

  /**
   * Gets the collaborating organizations.
   * 
   * @return the collaboratingOrganizations
   */
  public List<String> getCollaboratingOrganizations() {
    return collaboratingOrganizations;
  }

  /**
   * Sets the collaborating organizations.
   * 
   * @param collaboratingOrganizations the collaboratingOrganizations to set
   */
  public void setCollaboratingOrganizations(List<String> collaboratingOrganizations) {
    this.collaboratingOrganizations = collaboratingOrganizations;
  }

  /**
   * Gets the outcome types.
   * 
   * @return the outcomeTypes
   */
  public List<String> getOutcomeTypes() {
    return outcomeTypes;
  }

  /**
   * Sets the outcome types.
   * 
   * @param outcomeTypes the outcomeTypes to set
   */
  public void setOutcomeTypes(List<String> outcomeTypes) {
    this.outcomeTypes = outcomeTypes;
  }

  /**
   * Gets the years.
   * 
   * @return the years
   */
  public List<String> getYears() {
    return years;
  }

  /**
   * Sets the years.
   * 
   * @param years the years to set
   */
  public void setYears(List<String> years) {
    this.years = years;
  }

  /**
   * Loads a record on to the form ready for editing.
   * 
   * @return resolution of next page.
   */
  @DontValidate
  public Resolution preEdit() {
    Model model = Model.getInstance();

    // Set organization list in session.
    List<MyOrganization> results = model.listOrganizations();
    List<String> list = new ArrayList<String>();
    for (MyOrganization record : results) {
      list.add(record.getName());
    }
    getContext().getRequest().getSession().setAttribute("organizations", list);

    // Get collaboration info and populate vars.
    MyCollaboration collaboration = new MyCollaboration();
    if (this.name != null) {
      collaboration = model.getCollaboration(this.name);
    }
    if (collaboration == null) {
      collaboration = new MyCollaboration();
      collaboration.setName(this.name);
    }
    this.setDescription(collaboration.getDescription());
    this.setCollaboratingOrganizations(collaboration.getOrganizations());
    this.setYears(collaboration.getYears());
    this.setCollaborationTypes(collaboration.getTypes());
    this.setOutcomeTypes(collaboration.getOutcomes());
    return new RedirectResolution("/editCollaboration.jsp").flash(this);
  }

  /**
   * Delete record.
   * 
   * @return resolution back to same page.
   */
  public Resolution delete() {
    this.model.deleteCollaboration(this.name);
    return new RedirectResolution("/selectCollaboration.jsp").flash(this);
  }

  /**
   * Add new organization to collaboration org list.
   * 
   * @return resolution back to edit collaboration jsp.
   */
  @DontValidate
  public Resolution addCollaboratingOrganizaton() {
    List<String> list = this.getCollaboratingOrganizations();
    if ((this.newCollaboratingOrganization != null)
        && (!this.newCollaboratingOrganization.isEmpty())) {
      Boolean found = false;
      for (String item : this.collaboratingOrganizations) {
        if (this.newCollaboratingOrganization.equalsIgnoreCase(item)) {
          found = true;
        }
      }
      if (!found) {
        list.add(this.newCollaboratingOrganization);
      }
    }
    this.setCollaboratingOrganizations(list);
    this.newCollaboratingOrganization = "";
    return new RedirectResolution("/editCollaboration.jsp").flash(this);
  }

  /**
   * Add new type to collaboration types list.
   * 
   * @return resolution back to edit collaboration jsp.
   */
  @DontValidate
  public Resolution addCollaborationType() {
    Log.enter("EditCollaborationActionBean.addCollaboratingType start.");
    List<String> list = this.getCollaborationTypes();
    Integer count = list.size();
    Log.enter("getCollaboratingTypes count =" + count.toString());
    if ((this.newCollaborationType != null) && (!this.newCollaborationType.isEmpty())) {
      Boolean found = false;
      Log.enter("type not null or empty");
      for (String item : this.collaborationTypes) {
        if (this.newCollaborationType.equalsIgnoreCase(item)) {
          found = true;
        }
      }
      Log.enter("found = " + found.toString());
      if (!found) {
        list.add(this.newCollaborationType);
      }
    }
    this.setCollaborationTypes(list);
    this.newCollaborationType = "";
    Log.enter("EditCollaborationActionBean.addCollaboratingType finish.");
    return new RedirectResolution("/editCollaboration.jsp").flash(this);
  }

  /**
   * Add new outcome type to collaboration outcomes list.
   * 
   * @return resolution back to edit collaboration jsp.
   */
  @DontValidate
  public Resolution addOutcomeType() {
    List<String> list = this.getOutcomeTypes();
    if ((this.newOutcomeType != null) && (!this.newOutcomeType.isEmpty())) {
      Boolean found = false;
      for (String item : this.outcomeTypes) {
        if (this.newOutcomeType.equalsIgnoreCase(item)) {
          found = true;
        }
      }
      if (!found) {
        list.add(this.newOutcomeType);
      }
    }
    this.setOutcomeTypes(list);
    this.newOutcomeType = "";
    return new RedirectResolution("/editCollaboration.jsp").flash(this);
  }

  /**
   * Saves the record.
   * 
   * @return resolution to main menu.
   */
  public Resolution save() {
    MyCollaboration collaboration = new MyCollaboration();
    collaboration.setName(this.getName());
    collaboration.setDescription(this.getDescription());
    collaboration.setOrganizations(this.getCollaboratingOrganizations());
    collaboration.setYears(this.getYears());
    collaboration.setTypes(this.getCollaborationTypes());
    collaboration.setOutcomes(this.getOutcomeTypes());
    this.model.saveCollaboration(collaboration);
    return new RedirectResolution("/selectCollaboration.jsp").flash(this);
  }

  /**
   * Returns to main menu and does not save data.
   * 
   * @return resolution to main menu.
   */
  @DontValidate
  public Resolution cancel() {
    return new RedirectResolution("/selectCollaboration.jsp");
  }

  /**
   * Clear data.
   * 
   * @return resolution back to same page.
   */
  @DontValidate
  public Resolution clearData() {
    Model.getInstance().clearCollaborations();
    return new RedirectResolution("/selectCollaboration.jsp").flash(this);
  }

  /**
   * Imports xml data.
   * 
   * @return resolution back to same page.
   */
  @DontValidate
  public Resolution importData() {
    Model.getInstance().importCollaborations(this.importFile);
    return new RedirectResolution("/selectCollaboration.jsp").flash(this);
  }

  /**
   * Exports xml data.
   * 
   * @return resolution back to same page.
   */
  @DontValidate
  public Resolution exportData() {
    Model.getInstance().exportCollaborations(this.exportFile);
    return new RedirectResolution("/selectCollaboration.jsp").flash(this);
  }

}
